import json
from datetime import datetime
from database_lib.testdb.test_model import TestModel
from decorator import close_db_connection, update_db_operation


class ApiStatistics(TestModel):
    def __init__(self):
        self.session = self.session()

    @update_db_operation
    def update_data_by_service(self, service,  time, api_count, case_count):
        self.session.query(self.ApiStatistics).filter_by(service=service, time=time).\
            update({'api_count': api_count, 'case_count': case_count})

    @update_db_operation
    def add_statistics(self, service, time, api_count, case_count):
        data = self.ApiStatistics(service=service, time=time, api_count=api_count, case_count=case_count)
        self.session.add(data)

    @close_db_connection
    def select_statistics_by_service(self, service, time):
        data = self.session.query(self.ApiStatistics).filter_by(service=service, time=time).first()
        return data
